package com.bytz.modules.cms.domain.impl.order;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.BooleanUtil;
import com.bytz.common.constant.BooleanConstant;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.order.constant.OrderConstants;
import com.bytz.modules.cms.order.model.OrderItemModel;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.product.constant.ProductConstants;
import com.bytz.modules.cms.product.entity.ProductEcLicenceUser;
import com.bytz.modules.cms.product.model.ProductModel;
import com.bytz.modules.cms.product.service.IProductEcLicenceUserService;
import com.bytz.modules.cms.reseller.util.ResellerSecurityUtils;
import com.bytz.modules.system.security.vo.LoginReseller;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 订单EC处理器 6
 * @author shanxuezhong
 */

public class MallOrderECProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {


    private IProductEcLicenceUserService ecLicenceUserService;

    @Autowired
    public void setEcLicenceUserService(IProductEcLicenceUserService ecLicenceUserService) {
        this.ecLicenceUserService = ecLicenceUserService;
    }

    public MallOrderECProcessor(int sequence) {
        super(sequence);
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        OrderModel order = context.getOrder();
        if (BooleanUtil.isFalse(context.getSubmit())) {
            return;
        }

        LoginReseller loginReseller = ResellerSecurityUtils.getLoginUser();
        if (loginReseller == null) {
            throw new BytzBootException("获取不到当前经销商");
        }

        // 是不是用户所有产品都要填EC
        if (BooleanConstant.isTrue(loginReseller.getEcRequired())) {
            order.setEcRequired(OrderConstants.EcRequired.NECESSARY);

            for (OrderItemModel orderItem : order.getOrderItemList()) {
                orderItem.setEcRequired(OrderConstants.EcRequired.NECESSARY);
            }

            order.setEcStatus(OrderConstants.EcStatus.UNCOMMITTED);

            return;
        }


        // 是不是没有有EC的产品
        Collection<ProductModel> productModels = context.getProducts();
        List<String> ecProductList = productModels.stream()
                .filter(p -> ProductConstants.ProductIsEC.YES.equals(p.getIsEc()))
                .map(ProductModel::getProductNumber)
                .collect(Collectors.toList());

        if (CollectionUtil.isEmpty(ecProductList)) {
            order.setEcRequired(OrderConstants.EcRequired.UNNECESSARY);

            for (OrderItemModel orderItem : order.getOrderItemList()) {
                orderItem.setEcRequired(OrderConstants.EcRequired.UNNECESSARY);
            }

            return;
        }

        // 再看有没有许可证
        Set<String> licenceProducts = ecLicenceUserService.getByCustomerAndProduct(order.getEndUserNumber(), ecProductList)
                .stream().map(ProductEcLicenceUser::getProductNumber).collect(Collectors.toSet());

        boolean necessary = false;
        for (OrderItemModel orderItem : order.getOrderItemList()) {
            // 任何一个商品没有找到许可证，均标记为需要提交ec
            if (licenceProducts.contains(orderItem.getProductNumber())) {
                orderItem.setEcRequired(OrderConstants.EcRequired.OPTIONAL);
            } else {
                orderItem.setEcRequired(OrderConstants.EcRequired.NECESSARY);
                necessary = true;
            }
        }

        order.setEcStatus(OrderConstants.EcStatus.UNCOMMITTED);
        order.setEcRequired(necessary ? OrderConstants.EcRequired.NECESSARY : OrderConstants.EcRequired.OPTIONAL);
    }
}